<div class="step">第二步：企业信息录入（共三步）</div>
<div class="form">
    <div class="form-group">
        <label class="require" for="company">单位名称</label>
        <input type="text" class="form-control form-control_lg" id="company" maxlength="100"><span style="line-height: 34px;display: none;" id="company_exists_area" ><a target="_blank" id="company_exists_url">公司已存在，点此查看</a></span>
    </div>
    <div class="form-group">
        <label class="require" for="reg_address">注册地</label>
        <select class="form-control form-control_sm" id="reg_province">
            <option value="">请选择</option>
            @forelse($provinceList as $key=>$value)
                <option value="{{$value['id']}}">{{$value['name']}}</option>
            @empty
            @endforelse
        </select>
        <select class="form-control form-control_sm" id="reg_city">
            <option value="">请选择</option>
        </select>
        <select class="form-control form-control_sm" id="reg_area">
            <option value="">请选择</option>
        </select>
    </div>
    <div class="form-group">
        <label class="radio">&nbsp;</label>
        <input type="text" class="form-control form-control_lg" id="reg_address" maxlength="200"
               placeholder="请输入详细地址">
    </div>
    <div class="form-group">
        <label class="require" for="legal">法人代表</label>
        <input type="text" class="form-control form-control_sm" id="legal" maxlength="20">
    </div>
    <div class="form-group">
        <label class="require" for="legal">涉及领域</label>
        <a class="btn j_setDomain">设置</a>
        <div class="domain j_domainItems">

        </div>
    </div>
    <div class="form-group domain_tech_flag">
        <label class="require" for="legal">相关技术路径</label>
        <a class="btn j_setPath">设置</a>
    </div>
    <div class="form-group domain_tech_flag">
        <label class="require" for="legal">服务能力</label>
        <table class="v3-table v3-table_tech j_techService" style="display: none">
            <thead>
            <tr>
                <th>技术路径名称</th>
                <th>评估咨询</th>
                <th>方案设计</th>
                <th>建设实施</th>
                <th>运维</th>
                <th>设备供应</th>
                <th>其他</th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
    <div class="form-group">
        <label for="org_code_cert">组织机构代码证号</label>
        <input type="text" class="form-control" id="org_code_cert" maxlength="20">
    </div>
    <div class="form-group">
        <label for="fileUploadOrg">组织机构代码证复印件</label>
        <input id="fileUploadOrg" class="form-control form-control_sm j_fileSelect" data-file="true" name="files" type="file">
        <input id="orgName" name="orgName" class="j_originalName" type="hidden" value="">
        <input id="orgUrl" name="orgUrl" class="j_remoteName" type="hidden" value="">
        <input id="orgThumbUrl" name="orgThumbUrl" class="j_remoteThumbName" type="hidden" value="">
        <input id="orgMd5" name="orgMd5" class="j_md5" type="hidden" value="">
    </div>
    <div class="form-group">
        <label for="fileUploadBl">营业执照复印件</label>
        <input id="fileUploadBl" class="form-control form-control_sm j_fileSelect" data-file="true" name="files" type="file">
        <input id="blName" name="blName" class="j_originalName" type="hidden" value="">
        <input id="blUrl" name="blUrl" class="j_remoteName" type="hidden" value="">
        <input id="blThumbUrl" name="blThumbUrl" class="j_remoteThumbName" type="hidden" value="">
        <input id="blMd5" name="blMd5" class="j_md5" type="hidden" value="">
    </div>
    <div class="form-group">
        <label class="require" for="email">Email地址</label>
        <input type="text" class="form-control" id="email" maxlength="100" placeholder="确保该邮箱可收到邮件">
    </div>
    <div class="form-group">
        <label class="radio">&nbsp;</label>
        <button type="button" class="btn j_submitCompany">下一步</button>
    </div>
</div>
<input id="j_domainIds" type="hidden" value="">
<input id="j_techPathIds" type="hidden" value="">

<div id="j_domainModal"></div>
<div id="j_pathModal"></div>

<script>
    (function ($) {
        $(function () {
            $("#company").on('blur',function(){
                var company_name = $(this).val();
                $.ajax({
                    url:'/company_name_auth',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    data: {
                        companyName:company_name
                    },
                    type: 'post',
                    dateType: 'json',
                    success:function(data){
                        var result = JSON.parse(data);
                        if (result.result == 1) {
                            $("#company_exists_area").show();
                            $("#company_exists_url").attr('href','/company_info?id='+result.company_id);
                        }else{
                            $("#company_exists_area").hide();
                        }
                    }

                })
            });
            $('.j_submitCompany').on('click', function () {
                var $btn = $(this);
                var maxlen = 0;

                var $company = $("#company");
                maxlen = $company.attr('maxlength');
                var company = $company.val();
                if (!company || company.length > maxlen) {
                    return showErr($company, "请输入单位名称，" + maxlen + "字以内");
                }
                var $reg_province = $("#reg_province");
                var reg_province = $reg_province.val();
                if (reg_province) {
                    //return showErr($reg_province, "请选择省");
                }
                var $reg_city = $("#reg_city");
                var reg_city = $reg_city.val();
                if (reg_city) {
                    //return showErr($reg_city, "请选择市");
                }
                var $reg_area = $("#reg_area");
                var reg_area = $reg_area.val();
                if (reg_area) {
                    //return showErr($reg_area, "请选择区");
                }
                var $reg_address = $("#reg_address");
                maxlen = $reg_address.attr('maxlength');
                var reg_address = $reg_address.val();
                if (!reg_address || reg_address.length > maxlen) {
                    return showErr($reg_address, "请输入详细地址，" + maxlen + "字以内");
                }
                var $legal = $("#legal");
                maxlen = $legal.attr('maxlength');
                var legal = $legal.val();
                if (!legal || legal.length > maxlen) {
                    return showErr($legal, "请输入法人地址，" + maxlen + "字以内");
                }
                var domainIds = $('#j_domainIds').val();
                if (!domainIds || domainIds == ',') {
                    return showErr($(".j_setDomain"), "请设置涉及领域");
                }
                if ($(".domain_tech_flag").css('display') =='none') {
                    techPath = [];
                }else{
                    var pathIds = $('#j_techPathIds').val();
                    if (!pathIds || pathIds == ',') {
                        return showErr($(".j_setDomain"), "请设置相关技术路径");
                    }
                    var techPath = [];
                    var $techTr = $('.j_techService tbody tr');
                    for (var i = 0, l = $techTr.length; i < l; i++) {
                        techPath.push({
                            'id': $techTr.eq(i).find('.j_techPathId').val(),
                            'key': [$techTr.eq(i).find('input[type="checkbox"]').eq(0).prop('checked'), $techTr.eq(i).find('input[type="checkbox"]').eq(1).prop('checked'), $techTr.eq(i).find('input[type="checkbox"]').eq(2).prop('checked'), $techTr.eq(i).find('input[type="checkbox"]').eq(3).prop('checked'), $techTr.eq(i).find('input[type="checkbox"]').eq(4).prop('checked'), $techTr.eq(i).find('input[type="checkbox"]').eq(5).prop('checked')]
                        });
                    }
                }



                var $org_code_cert = $("#org_code_cert");
                var org_code_cert = $org_code_cert.val();


                //组织机构代码（扫描件）
                var organize_img_name = $("#orgName").val();
                var organize_img_url = $("#orgUrl").val();
                var organize_img_md5 = $("#orgMd5").val();
                var organize_img_thumbUrl = $("#orgThumbUrl").val();
                var organize_code_file = "";
                if (organize_img_url) {
                    organize_code_file = [
                        {
                            "name": organize_img_name,
                            "url": organize_img_url,
                            "md5": organize_img_md5,
                            "thumbUrl": organize_img_thumbUrl
                        }
                    ];
                }
                // if (!organize_code_file) {
                //     return showErr($("#organize_info"), "请上传组织机构代码（扫描件）");
                // }

                //营业执照注册号（扫描件）
                var number_img_name = $("#blName").val();
                var number_img_url = $("#blUrl").val();
                var number_img_md5 = $("#blMd5").val();
                var number_img_thumbUrl = $("#blThumbUrl").val();
                var organize_regist_number_file = "";
                if (number_img_url) {
                    organize_regist_number_file = [
                        {
                            "name": number_img_name,
                            "url": number_img_url,
                            "md5": number_img_md5,
                            "thumbUrl": number_img_thumbUrl
                        }
                    ];
                }
                // if (!organize_regist_number_file) {
                //     return showErr($("#number_info"), "请上传营业执照注册号（扫描件）");
                // }

                var $email = $("#email");
                maxlen = $email.attr('maxlength');
                var email = $email.val();
                if (!email || email.length > maxlen||!isEmail(email)) {
                    return showErr($email, "请输入Email地址，" + maxlen + "字以内");
                }




                $.ajax({
                    url: "/add_company_info",
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    data: {
                        company: company,
                        regist_address_id:reg_province+','+reg_city+','+reg_area,
                        regist_address:reg_address,
                        legal_person:legal,
                        domain:domainIds,
                        tech_path:techPath,
                        organize_code:org_code_cert,
                        organize_code_file:organize_code_file,
                        regist_number_file:organize_regist_number_file,
                        email:email
                    },
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        var result = data["result"];
                        var msg = data["msg"];
                        if (result === 0) {
                            //第一步完成显示第二步页面
                            $("#j_sign").html('<div class="v3-form"><div class="step">第三步：邮箱验证（共三步）</div><div class="verify">一封验证邮件已发送至您填写的Email地址，请到您的邮箱完成验证。</div></div>');
                        } else {
                            return showErr($btn, msg);
                        }
                    },
                    error: function (data) {
                        return showErr($btn, '提交失败');
                    }
                });
            });

            $(".j_fileSelect").on("change", function () {
                var $t = $(this);
                var multi = $t.attr("data-multi");
                var file = $t.val();
                if (file.indexOf(".") === -1) {
                    alert("图片格式有误，只支持.jpg .jpeg .bmp .gif .png .pdf");
                    return false;
                }
                checkImgSize(this, file);

                var ext = file.split(".");
                ext = ext[ext.length - 1].toLowerCase();
                var id = $t.attr("id");
                var $p = $t.parent();
                if (file && (ext == "jpg" || ext == "jpeg" || ext == "bmp" || ext == "gif" || ext == "png" || ext == "pdf")) {
                    $.ajaxFileUpload({
                        url: '/fileUpload', /* 上传文件路径 */
                        type: 'post',
                        secureuri: false,
                        fileElementId: id, /* file唯一id */
                        dataType: 'json',
                        data: {
                            files: file      /* 参数：上传的文件 */
                        },
                        success: function (data) {
                            $p.find(".j_originalName").val(data["name"]);
                            $p.find(".j_remoteName").val(data["url"]);
                            $p.find(".j_remoteThumbName").val(data["remote_thumbname"]);
                            $p.find(".j_md5").val(data["md5"]);
                            $t.val("");
                            /* 清空上传域 */
                        },
                        error: function (data, status, e) {
                            alert(e);
                        }
                    });
                } else {
                    alert("图片格式有误，只支持jpg，png");
                }
            });

            $('#j_sign').on('change', '#reg_province,#reg_city', function () {
                //省市联动
                var $t = $(this);
                var id = $t.val();
                if (id) {
                    $t.nextAll("select").html('<option value="">请选择</option>');
                    getArea(id, $t.next());
                }
            }).on('click', '.j_setDomain', function () {
                //设置领域
                var $modal = $('#j_domainModal');
                if (!$modal.find('.modal').length) {
                    $.get('/add_company_info_domain_page', function (data) {
                        $modal.html(data).css('display', 'block');
                    }, 'html');
                } else {
                    $modal.css('display', 'block');
                }
            }).on('click', '.j_setPath', function () {
                //设置技术路径
                if (!$('#j_domainIds').val()) {
                    return showErr($('.j_setDomain'), '请先设置涉及领域');
                }
                var $modal = $('#j_pathModal');
                var domainIdsData = $("#j_domainIds").val();
                if (!$modal.find('.modal').length) {
                    $.get('/add_company_info_tech_path',{domain:domainIdsData}, function (data) {
                        $modal.html(data).css('display', 'block');
                    }, 'html');
                } else {
                    $modal.css('display', 'block');
                }
            });

            function getArea(id, $p) {
                $.ajax({
                    url: '/select_change',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    data: {
                        id: id
                    },
                    type: 'post',
                    dateType: 'json',
                    success: function (data) {
                        var result = JSON.parse(data);
                        var optHtml = '<option value="">请选择</option>';
                        for (var i = 0, l = result.length; i < l; i++) {
                            optHtml += '<option value="' + result[i].id + '">' + result[i].name + '</option>';
                        }
                        $p.html(optHtml);
                    }
                })
            }
            //图片大小限制
            function checkImgSize($t, file, max) {
                var file_size = 0;
                if (!max) {
                    max = 1;
                }
                if ($.browser.msie) {
                    var img = new Image();
                    img.src = file;
                    if (img.fileSize > max * 1024 * 1024) {
                        alert("图片不大于" + max + "MB！");
                        return false;
                    }
                } else {
                    file_size = $t.files[0].size;
                    var size = file_size / 1024;
                    if (size > max * 1024) {
                        alert("图片不大于" + max + "MB！");
                        return false;
                    }
                }
            }
            //关闭模态框
            $('#j_domainModal,#j_pathModal').on('click', '.close', function () {
                $(this).parents('.modal').parent().fadeOut(200);
            });

        });
    })(jQuery);
</script>
